package com.zhilianbao.leyaogo.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.bql.variousbanner.anim.select.ZoomInEnter;
import com.bql.variousbanner.widget.loopviewpager.ViewPagerCompat;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.ui.activity.base.BaseOkHttpActivity;
import com.zhilianbao.leyaogo.ui.activity.home.SelectSupplierActivity;
import com.zhilianbao.leyaogo.utils.AnimationUtils;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.widgets.UserGuideBanner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * ClassName: UserGuideActivity <br>
 * Description: <br>
 * Author: Cyarie <br>
 * Created: 2016/12/30 15:29 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class UserGuideActivity extends BaseOkHttpActivity {

    @BindView(R.id.userGuide_banner)
    UserGuideBanner mUserGuideBanner;

    private List<View> guideViews;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_user_guide;
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        guideViews = new ArrayList<>();
        View guide1 = getLayoutInflater().inflate(R.layout.view_guide_0, null);
        View guide2 = getLayoutInflater().inflate(R.layout.view_guide_1, null);
        View guide3 = getLayoutInflater().inflate(R.layout.view_guide_2, null);
        guideViews.add(guide1);
        guideViews.add(guide2);
        guideViews.add(guide3);
        mUserGuideBanner
                .setIndicatorWidth(6)
                .setIndicatorHeight(6)
                .setIndicatorGap(12)
                .setIndicatorCornerRadius(3.5f)
                .setSelectAnimClass(ZoomInEnter.class)
                //                .setTransformerClass(DepthTransformer.class)
                .barPadding(0, 10, 0, 10)
                .setSource(guideViews)
                .startScroll();

        mUserGuideBanner.setOnJumpClickListener(() -> {
                    jumpClearAnim3();
                    Utils.putFirstEnter(false);
                    Bundle bundle = new Bundle();
                    bundle.putBoolean(IntentConfig.FIRST_SELECT_SUPPLIER, true);
                    Utils.startActivity(this, SelectSupplierActivity.class, bundle);
                    finish();
                }
        );
        mUserGuideBanner.getViewPager().setOverScrollMode(View.OVER_SCROLL_NEVER);
        mUserGuideBanner.addOnPageChangeListener(new ViewPagerCompat.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                startAnimation(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        startAnimation(0);
    }

    /**
     * 执行动画效果
     *
     * @param position
     */
    private void startAnimation(int position) {
        View contentView = guideViews.get(position);
        View animPhone = null;
        View cloud1 = null;
        View cloud2 = null;
        View cloud3 = null;
        View cloud4 = null;
        switch (position) {
            case 0:
                animPhone = contentView.findViewById(R.id.iv_bg_0_phone);
                cloud1 = contentView.findViewById(R.id.iv_cloud_0_0);
                cloud2 = contentView.findViewById(R.id.iv_cloud_0_1);
                cloud3 = contentView.findViewById(R.id.iv_cloud_0_2);
                hideView(1);
                hideView(2);
                break;
            case 1:
                animPhone = contentView.findViewById(R.id.iv_bg_1_phone);
                cloud1 = contentView.findViewById(R.id.iv_cloud_1_0);
                cloud2 = contentView.findViewById(R.id.iv_cloud_1_1);
                cloud3 = contentView.findViewById(R.id.iv_cloud_1_2);
                hideView(0);
                hideView(2);
                break;
            case 2:
                animPhone = contentView.findViewById(R.id.iv_bg_2_phone);
                cloud1 = contentView.findViewById(R.id.iv_cloud_2_0);
                cloud2 = contentView.findViewById(R.id.iv_cloud_2_1);
                cloud3 = contentView.findViewById(R.id.iv_cloud_2_2);
                cloud4 = contentView.findViewById(R.id.iv_cloud_2_3);
                hideView(0);
                hideView(1);
                break;
        }
        AnimationUtils.slideIn(AnimationUtils.DIRECTION_BOTTOM, AnimationUtils.Decelerate, animPhone, 800, 0, null);
        AnimationUtils.slideIn(AnimationUtils.DIRECTION_LEFT, AnimationUtils.Decelerate, cloud1, 800, 0, null);
        AnimationUtils.slideIn(position == 2 ? AnimationUtils.DIRECTION_LEFT : AnimationUtils.DIRECTION_RIGHT, AnimationUtils.Decelerate, cloud2, 800, 0, null);
        AnimationUtils.slideIn(AnimationUtils.DIRECTION_RIGHT, AnimationUtils.Decelerate, cloud3, 800, 0, null);
        AnimationUtils.slideIn(AnimationUtils.DIRECTION_RIGHT, AnimationUtils.Decelerate, cloud4, 800, 0, null);

    }

    /**
     * 点击立即体验清除动画
     */
    private void jumpClearAnim3() {
        View contentView = guideViews.get(2);
        View animPhone = contentView.findViewById(R.id.iv_bg_2_phone);
        View cloud1 = contentView.findViewById(R.id.iv_cloud_2_0);
        View cloud2 = contentView.findViewById(R.id.iv_cloud_2_1);
        View cloud3 = contentView.findViewById(R.id.iv_cloud_2_2);
        View cloud4 = contentView.findViewById(R.id.iv_cloud_2_3);
        clearAnim(animPhone);
        clearAnim(cloud1);
        clearAnim(cloud2);
        clearAnim(cloud3);
        clearAnim(cloud4);
    }

    /**
     * 清除动画
     *
     * @param view
     */
    private void clearAnim(View view) {
        if (view == null)
            return;
        view.clearAnimation();
    }


    /**
     * 隐藏View
     */
    private void hideView(int position) {
        View contentView = guideViews.get(position);
        View animPhone = null;
        View cloud1 = null;
        View cloud2 = null;
        View cloud3 = null;
        View cloud4 = null;
        switch (position) {
            case 0:
                animPhone = contentView.findViewById(R.id.iv_bg_0_phone);
                cloud1 = contentView.findViewById(R.id.iv_cloud_0_0);
                cloud2 = contentView.findViewById(R.id.iv_cloud_0_1);
                cloud3 = contentView.findViewById(R.id.iv_cloud_0_2);
                break;
            case 1:
                animPhone = contentView.findViewById(R.id.iv_bg_1_phone);
                cloud1 = contentView.findViewById(R.id.iv_cloud_1_0);
                cloud2 = contentView.findViewById(R.id.iv_cloud_1_1);
                cloud3 = contentView.findViewById(R.id.iv_cloud_1_2);
                break;
            case 2:
                animPhone = contentView.findViewById(R.id.iv_bg_2_phone);
                cloud1 = contentView.findViewById(R.id.iv_cloud_2_0);
                cloud2 = contentView.findViewById(R.id.iv_cloud_2_1);
                cloud3 = contentView.findViewById(R.id.iv_cloud_2_2);
                cloud4 = contentView.findViewById(R.id.iv_cloud_2_3);
                break;
        }
        stopAnim(animPhone);
        stopAnim(cloud1);
        stopAnim(cloud2);
        stopAnim(cloud3);
        stopAnim(cloud4);
    }

    /**
     * 停止动画
     */
    private void stopAnim(View view) {
        if (view == null)
            return;
        view.setVisibility(View.INVISIBLE);
        if (view.getAnimation() != null && !view.getAnimation().hasEnded())
            view.getAnimation().cancel();
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return true;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return TransitionMode.MAIN;
    }
}
